﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CreateTestPageOne : System.Web.UI.Page
{
    DataTable dt;
    //public static string testId,copyTestId;


    protected void Page_Load(object sender, EventArgs e)
    {
        //testId = Request.QueryString["TestId"];
        //copyTestId = Request.QueryString["CopyTestId"];
        if (!IsPostBack)
        {
            LoadSubject();

            if ((Request.QueryString["TestId"] != null) && (Request.QueryString["TestId"] != ""))
                setControlValues();
            if ((Request.QueryString["CopyTestId"] != null) && (Request.QueryString["CopyTestId"] != ""))
                setCopyControlValues();

            string val = Request.QueryString["previousPage"];
            if (val == "page2")
                setAllValue();
            if (val == "page3")
                setAllValuePageThree();
        }
    }

    private void LoadSubject()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        ddlSub.Items.Add("--Select One--");
        ddlSub.SelectedIndex = ddlSub.Items.IndexOf(ddlSub.Items.FindByText("--Select One--"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
    }
    protected void ddlSub_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblTopicsAvail.Items.Clear();
        if (ddlSub.SelectedItem.Text == "--Select One--")
            lablError.Text="Please Select One Subject";
        else
        {
            dt = new DataTable();
            dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
            lblTopicsAvail.Items.Add(new ListItem("All","0"));
            lblTopicSelect.Items.Add(new ListItem("All", "0"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][0].ToString();
                li.Value = dt.Rows[i][1].ToString();
                lblTopicsAvail.Items.Add(li);
            }
        }
    }
    protected void rdoAutoSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (rdoAutoSelect.Checked == true)
        {
            lablNoOfQuestion.Visible = true;
            lablPattern.Visible = true;
            lablHigh.Visible = true;
            lablMedium.Visible = true;
            lablLow.Visible = true;
            lablCutoff.Visible = true;

            txtNoOfQuestion.Visible = true;
            txtHigh.Visible = true;
            txtMedium.Visible = true;
            txtLow.Visible = true;
            txtCuttOff.Visible = true;
        }
        else
        {
            lablNoOfQuestion.Visible = false;
            lablPattern.Visible = false;
            lablHigh.Visible = false;
            lablMedium.Visible = false;
            lablLow.Visible = false;
            lablCutoff.Visible = false;

            txtNoOfQuestion.Visible = false;
            txtHigh.Visible = false;
            txtMedium.Visible = false;
            txtLow.Visible = false;
            txtCuttOff.Visible = false;
        }
    }
    protected void rdoManualSelect_CheckedChanged(object sender, EventArgs e)
    {
        lablNoOfQuestion.Visible = false;
        lablPattern.Visible = false;
        lablHigh.Visible = false;
        lablMedium.Visible = false;
        lablLow.Visible = false;
        lablCutoff.Visible = false;

        txtNoOfQuestion.Visible = false;
        txtHigh.Visible = false;
        txtMedium.Visible = false;
        txtLow.Visible = false;
        txtCuttOff.Visible = false;
    }

    private void clearAll()
    {
        txtTestName.Text = "";
        ddlSub.SelectedIndex = -1;
        lblTopicsAvail.Items.Clear();
        lblTopicSelect.Items.Clear();
        txtHigh.Text = "";
        txtMedium.Text = "";
        txtLow.Text = "";
        txtNoOfQuestion.Text = "";
        txtCuttOff.Text = "";
        rdoAutoSelect.Checked = false;
        rdoAutoSelect.Checked = true;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (lblTopicSelect.Items.Count == 0)
            lablError.Text="Topics not present";
        else if (lblTopicSelect.SelectedIndex == -1)
            lablError.Text = "First Select One Topic";
        else
        {
            if (lblTopicSelect.SelectedValue == "0")
            {
                lblTopicSelect.Items.Clear();
                lblTopicSelect.Items.Add(new ListItem("All", "0"));
            }
            else
                lblTopicSelect.Items.RemoveAt(lblTopicSelect.Items.IndexOf(lblTopicSelect.SelectedItem));
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bool flag = true;
        if (lblTopicsAvail.Items.Count == 0)
            lablError.Text="First Add topics";
        else if (lblTopicsAvail.SelectedIndex == -1)
            lablError.Text = "First Select One Topic";
        else
        {
            if (lblTopicsAvail.SelectedValue == "0")
            {
                if (lblTopicSelect.Items.Count == 1)
                {
                    for (int k = 1; k < lblTopicsAvail.Items.Count; k++)
                    {
                        lblTopicSelect.Items.Add(new ListItem(lblTopicsAvail.Items[k].Text, lblTopicsAvail.Items[k].Value));
                    }
                }
                else if (lblTopicSelect.Items.Count > 1)
                {
                    for (int i = 0; i < lblTopicsAvail.Items.Count; i++)
                    {
                        for (int j = 0; j < lblTopicSelect.Items.Count; j++)
                        {
                            if (lblTopicsAvail.Items[i].Value != lblTopicSelect.Items[j].Value)
                            {
                                if (isAvailable(lblTopicsAvail.Items[i].Value))
                                    lblTopicSelect.Items.Add(new ListItem(lblTopicsAvail.Items[i].Text, lblTopicsAvail.Items[i].Value));

                            }
                        }
                    }
                }

            }
            else
            {
                for (int k = 0; k < lblTopicSelect.Items.Count; k++)
                {
                    if (lblTopicsAvail.SelectedValue.ToString() == lblTopicSelect.Items[k].Value.ToString())
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == true)
                    lblTopicSelect.Items.Add(new ListItem(lblTopicsAvail.SelectedItem.Text, lblTopicsAvail.SelectedItem.Value));
            }
        }
    }

    private bool isAvailable(string value)
    {
        bool flag = true;
        for (int k = 0; k < lblTopicSelect.Items.Count; k++)
        {
            if (value == lblTopicSelect.Items[k].Value)
            {
                flag = false;
                break;
            }

        }
        return flag;
    }
    private bool validation()
    {
        if (lblTopicSelect.Items.Count < 1)
        {
            lablError.Text="First Select some Topics";
            return false;
        }
        else if ((txtNoOfQuestion.Text == "")&&(rdoAutoSelect.Checked==true))
        {
            lablError.Text = "Please enter total no of question";
            return false;
        }
        else if (rdoAutoSelect.Checked == true)
        {
            if ((txtLow.Text == "") || (txtMedium.Text == "") || (txtHigh.Text == ""))
            {
                lablError.Text = "No of question in high,medium and low cannot be null";
                return false;
            }
            if (!(Convert.ToInt32(txtHigh.Text) + Convert.ToInt32(txtMedium.Text) + Convert.ToInt32(txtLow.Text) == Convert.ToInt32(txtNoOfQuestion.Text)))
            {
                lablError.Text = "No of question must be equal to sum of high,medium and low";
                return false;
            }
            if (txtCuttOff.Text == "")
            {
                lablError.Text = "Please specify cutoff marks";
                return false;
            }
            if (Convert.ToInt32(txtCuttOff.Text) > Convert.ToInt32(txtNoOfQuestion.Text))
            {
                lablError.Text = "Cutoff marks can not be greater than no of question specified";
                return false;
            }

        }
        return true;
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        string topic = "";
        if (validation() == true)
        {
            Information.testName = txtTestName.Text;
            Information.subjectId = Convert.ToInt32(ddlSub.SelectedItem.Value);
            for (int i = 1; i < lblTopicSelect.Items.Count; i++)
            {
                topic += lblTopicSelect.Items[i].Value + "|";
            }
            Information.topicId = topic;
            Information.duration = Convert.ToInt32(txtDuration.Text);
            if (rdoManualSelect.Checked == true)
                Information.isManualSelection = 1;
            else
            {
                Information.isManualSelection = 0;
                Information.totalNoOfQuestion = Convert.ToInt32(txtNoOfQuestion.Text);
                Information.highQuestion = Convert.ToInt32(txtHigh.Text);
                Information.mediumQuestion = Convert.ToInt32(txtMedium.Text);
                Information.lowQuestion = Convert.ToInt32(txtLow.Text);
                Information.cuttOff = Convert.ToInt32(txtCuttOff.Text);
            }
            if (rdoAutoSelect.Checked == true)
            {
                if(Request.QueryString["CopyTestId"]!=null)
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page1&CopyTestId=" + Request.QueryString["CopyTestId"]);
                else if(Request.QueryString["TestId"]!=null)
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page1&TestId=" + Request.QueryString["TestId"]);
                else
                    Response.Redirect("~/Admin/CreateTestPageThree.aspx?fromPage=page1");
            }
            else if (rdoManualSelect.Checked == true)
            {
               if(Request.QueryString["CopyTestId"]!=null)
                   Response.Redirect("~/Admin/CreateTestPageTwo.aspx?CopyTestId=" + Request.QueryString["CopyTestId"]);
                else if (Request.QueryString["TestId"] != null)
                   Response.Redirect("~/Admin/CreateTestPageTwo.aspx?TestId=" + Request.QueryString["TestId"]);
                else
                   Response.Redirect("~/Admin/CreateTestPageTwo.aspx");
            }
        }
    }
    protected void txtTestName_TextChanged(object sender, EventArgs e)
    {
        lablErrorTestName.Text = "";
        dt = new DataTable();
        dt = AdminCreateTestDAL.getTestNames();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtTestName.Text.ToLower() == dt.Rows[i][0].ToString().ToLower())
            {
                lablErrorTestName.Text="Please Enter Diffrent Test Name";
                txtTestName.Text = "";
                txtTestName.Focus();
                break;
            }
        }
    }

    private void LoadAllSub()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            ddlSub.Items.Add(li);
        }
    }


    private void setAllValue()
    {
        txtTestName.Text = Information.testName;
        ddlSub.Items.Clear();
        LoadAllSub();
        ddlSub.Items.FindByValue(Information.subjectId.ToString()).Selected = true;
        dt = new DataTable();
        dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
        lblTopicsAvail.Items.Clear();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            lblTopicsAvail.Items.Add(li);
        }

        DataTable dtTopics;
        string topicId = Information.topicId;
        string[] AllTopicId = topicId.Split('|');
        lblTopicSelect.Items.Clear();
        for (int i = 0; i < AllTopicId.Length-1; i++)
        {
            dtTopics = AdminCreateTestDAL.GetTopicName(Convert.ToInt32(AllTopicId[i]));
         
            ListItem li = new ListItem();
            li.Text = dtTopics.Rows[0][0].ToString();
            li.Value = AllTopicId[i];
            lblTopicSelect.Items.Add(li);

        }
        rdoManualSelect.Checked = true;
        txtDuration.Text = Information.duration.ToString();
    }


    private void setAllValuePageThree()
    {
        txtTestName.Text = Information.testName;
        ddlSub.Items.Clear();
        LoadAllSub();
        ddlSub.Items.FindByValue(Information.subjectId.ToString()).Selected = true;
        dt = new DataTable();
        dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            lblTopicsAvail.Items.Add(li);
        }

        DataTable dtTopics;
        string topicId = Information.topicId;
        string[] AllTopicId = topicId.Split('|');
        for (int i = 0; i < AllTopicId.Length-1; i++)
        {
            dtTopics = AdminCreateTestDAL.GetTopicName(Convert.ToInt32(AllTopicId[i]));

            ListItem li = new ListItem();
            li.Text = dtTopics.Rows[0][0].ToString();
            li.Value = AllTopicId[i];
            lblTopicSelect.Items.Add(li);

        }
            rdoAutoSelect.Checked = true;

            lablNoOfQuestion.Visible = true;
            lablPattern.Visible = true;
            lablHigh.Visible = true;
            lablMedium.Visible = true;
            lablLow.Visible = true;
            lablCutoff.Visible = true;

            txtNoOfQuestion.Visible = true;
            txtHigh.Visible = true;
            txtMedium.Visible = true;
            txtLow.Visible = true;
            txtCuttOff.Visible = true;

            txtNoOfQuestion.Text = Information.totalNoOfQuestion.ToString();
            txtHigh.Text = Information.highQuestion.ToString();
            txtMedium.Text = Information.mediumQuestion.ToString();
            txtLow.Text = Information.lowQuestion.ToString();
            txtCuttOff.Text = Information.cuttOff.ToString();
            txtDuration.Text = Information.duration.ToString();
     }


    private void setControlValues()
    {
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));
        txtTestName.Text = dtTest.Rows[0][1].ToString();
        ddlSub.Items.Clear();
        LoadAllSub();
        ddlSub.Items.FindByValue(dtTest.Rows[0][2].ToString()).Selected = true;

        dt = new DataTable();
        dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));

        lblTopicsAvail.Items.Add(new ListItem("All", "0"));
        lblTopicSelect.Items.Add(new ListItem("All", "0"));

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            lblTopicsAvail.Items.Add(li);
        }

        DataTable dtTopics;
        dtTopics = AdminCreateTestDAL.GetTopicNameBasedOnTestId(Convert.ToInt32(Request.QueryString["TestId"]));

        for (int i = 0; i < dtTopics.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dtTopics.Rows[i][0].ToString();
            li.Value = dtTopics.Rows[i][1].ToString();
            lblTopicSelect.Items.Add(li);

        }
        if (dtTest.Rows[0][3].ToString() == "1")
            rdoManualSelect.Checked = true;
        else
        {
            rdoManualSelect.Checked = false;
            rdoAutoSelect.Checked = true;

            lablNoOfQuestion.Visible = true;
            lablPattern.Visible = true;
            lablHigh.Visible = true;
            lablMedium.Visible = true;
            lablLow.Visible = true;
            lablCutoff.Visible = true;

            txtNoOfQuestion.Visible = true;
            txtHigh.Visible = true;
            txtMedium.Visible = true;
            txtLow.Visible = true;
            txtCuttOff.Visible = true;

            txtNoOfQuestion.Text = dtTest.Rows[0][4].ToString();
            txtHigh.Text = dtTest.Rows[0][5].ToString();
            txtMedium.Text = dtTest.Rows[0][6].ToString();
            txtLow.Text = dtTest.Rows[0][7].ToString();
            txtCuttOff.Text = dtTest.Rows[0][8].ToString();
        }
        txtDuration.Text = dtTest.Rows[0][12].ToString();

    }

    private void setCopyControlValues()
    {
        DataTable dtTest = new DataTable();
        dtTest = AdminCreateTestDAL.GetTestInfoBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));
        txtTestName.Text = "Copy of "+ dtTest.Rows[0][1].ToString();
        //lablErrorTestName.Text = "Please Give Test Name";
        ddlSub.Items.Clear();
        LoadAllSub();
        ddlSub.Items.FindByValue(dtTest.Rows[0][2].ToString()).Selected = true;

        dt = new DataTable();
        dt = AdminCreateTestDAL.getTopicRelatedToSubject(Convert.ToInt32(ddlSub.SelectedItem.Value));

        lblTopicsAvail.Items.Add(new ListItem("All", "0"));
        lblTopicSelect.Items.Add(new ListItem("All", "0"));

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dt.Rows[i][0].ToString();
            li.Value = dt.Rows[i][1].ToString();
            lblTopicsAvail.Items.Add(li);
        }

        DataTable dtTopics;
        dtTopics = AdminCreateTestDAL.GetTopicNameBasedOnTestId(Convert.ToInt32(Request.QueryString["CopyTestId"]));

        for (int i = 0; i < dtTopics.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = dtTopics.Rows[i][0].ToString();
            li.Value = dtTopics.Rows[i][1].ToString();
            lblTopicSelect.Items.Add(li);

        }
        if (dtTest.Rows[0][3].ToString() == "1")
            rdoManualSelect.Checked = true;
        else
        {
            rdoManualSelect.Checked = false;
            rdoAutoSelect.Checked = true;

            lablNoOfQuestion.Visible = true;
            lablPattern.Visible = true;
            lablHigh.Visible = true;
            lablMedium.Visible = true;
            lablLow.Visible = true;
            lablCutoff.Visible = true;

            txtNoOfQuestion.Visible = true;
            txtHigh.Visible = true;
            txtMedium.Visible = true;
            txtLow.Visible = true;
            txtCuttOff.Visible = true;

            txtNoOfQuestion.Text = dtTest.Rows[0][4].ToString();
            txtHigh.Text = dtTest.Rows[0][5].ToString();
            txtMedium.Text = dtTest.Rows[0][6].ToString();
            txtLow.Text = dtTest.Rows[0][7].ToString();
            txtCuttOff.Text = dtTest.Rows[0][8].ToString();
        }
        txtDuration.Text = dtTest.Rows[0][12].ToString();

    }



}